package com.luhui.codereview.token

import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.Messages
import com.luhui.codereview.api.ApiManager
import com.luhui.codereview.utils.IpUtils
import com.luhui.codereview.utils.RespBody
import com.luhui.codereview.utils.SerializableUtils
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.awt.EventQueue
import java.io.File


/**
 * @author luhui
 * @version 1.0
 * @date 2019/11/9 23:13
 */
class LocalUserManager{

    companion object {
        /**
         * 保存本地用户方法
         */
        fun saveLocalUserInfo(){
            var localUserInfo = if(LocalUserInfoHodler.localUserInfo==null){
                LocalUserInfo()
            }else{
                LocalUserInfoHodler.localUserInfo!!
            }
            LocalUserInfoHodler.localUserInfo = localUserInfo

            // 保存/修改用户信息至服务器端
            val macAddress = IpUtils.getMacAddress()
            if(localUserInfo.macAddress==null){
                localUserInfo.macAddress = macAddress
            }else{
                if(macAddress!=localUserInfo.macAddress){
                    Messages.showErrorDialog("身份不合法，无法修改用户信息！","保存失败！")
                }
            }

            GlobalScope.launch{
                 val userInfo = ApiManager.getApiInstance().saveUserInfo(localUserInfo)
                EventQueue.invokeLater {
                    Messages.showInfoMessage("保存成功！", "保存成功！")
                }
                localUserInfo.id = userInfo.body

                val savePathDirFile = File(File(System.getProperty("user.home")),"code-review-plugins")
                if(!savePathDirFile.exists()){
                    savePathDirFile.mkdirs()
                }
                val saveFileFile = File(savePathDirFile,"localUserInfo.dat")
                if(saveFileFile.exists()){
                    saveFileFile.delete()
                }
                val savePath  = saveFileFile.absolutePath
                //将新的用户信息序列化至硬盘
                SerializableUtils.serilalizeObject(localUserInfo,savePath)
            }
        }


        fun initUserInfo(project: Project) : LocalUserInfo?{
            //从本地文件中获取当前用户信息
            val file = File(File(System.getProperty("user.home")),"code-review-plugins/localUserInfo.dat")
            if(file.exists()){
                val retObj = SerializableUtils.deSerilializeObject(file.absolutePath)
                println("反序列化对象：$retObj")
                val localUserInfo = if(retObj is HashMap<*, *>){
                    //反序列化失败
                    val userInfo = LocalUserInfo()
                    userInfo.id = retObj["id"] as Long?
                    userInfo.realName = retObj["realName"] as String?
                    userInfo.email = retObj["email"] as String?
                    userInfo.macAddress = retObj["macAddress"] as String?
                    userInfo
                }else {
                    retObj as LocalUserInfo
                }
                LocalUserInfoHodler.localUserInfo = localUserInfo

                return localUserInfo
            }
            return null
        }

    }




}
